package ctrhost

import (
    "github.com/astaxie/beego/logs"
    "fmt"
    "net/url"
    "strings"
    "cloud_worm/util/umodels/mysqldb"
    "cloud_worm/util/umodels/mysqldb/dbdata"
    "cloud_worm/util/uhttp"
    "cloud_worm/wormweb/controllers/ctrconfig"
)

// @Description: machine alert 相关控制器
type MachineAlertController struct {
    ctrconfig.LoginCtr
}

/**
 @Description：先拉取所有的list
 @Param:
 @Return：
 */
func (m *MachineAlertController) AlertList() {
    if m.Guest() == false {
        return
    }

    ip := m.GetString("ip", mysqldb.SelectStrAll)
    if ip == mysqldb.SelectStrAll {
        logs.Error(fmt.Sprintf("input ip error."))
        data := map[string]interface{}{"total": 0, "data": []dbdata.WeaponAlertCount{}}
        m.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input ip error.", data)
        m.ServeJSON()
        return
    }

    status, err := m.GetInt("status", mysqldb.StatusAll)

    w := &dbdata.WeaponAlertCount{}
    count, err := w.SelectCount(status, ip)
    if err != nil {
        logs.Error(fmt.Sprintf("select for ip error: %s", err.Error()))
        data := map[string]interface{}{"total": 0, "data": []dbdata.WeaponAlertCount{}}
        m.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select for ip error.", data)
        m.ServeJSON()
        return
    }

    alertsCount, err := w.Select(mysqldb.SelectOffsetZero, mysqldb.SelectLimitAll, status, ip)
    if err != nil {
        logs.Error(fmt.Sprintf("select for ip error: %s", err.Error()))
        data := map[string]interface{}{"total": 0, "data": []dbdata.WeaponAlertCount{}}
        m.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select for ip error.", data)
        m.ServeJSON()
        return
    }

    data := map[string]interface{}{"total": count, "data": alertsCount}
    m.Data["json"] = uhttp.CreateOkResponse(data)
    m.ServeJSON()
    return
}

/**
 @Description：读取具体子项的详细信息
 @Param:
 @Return：
 */
func (m *MachineAlertController) parseURIForNameDetail() (string, error) {
    u, err := url.Parse(m.Ctx.Request.URL.EscapedPath())
    if err != nil {
        return "", err
    }
    inputURI := u.Path

    elems := strings.Split(inputURI, "/")
    if len(elems) <= 0 {
        return "", fmt.Errorf("input url error: %s", inputURI)
    }

    name := elems[len(elems) - 1]
    return name, nil
}

/**
 @Description：具体alert option的list信息
 @Param:
 @Return：
 */
func (m *MachineAlertController) AlertOptionList()  {
    if m.Guest() == false {
        return
    }

    ip := m.GetString("ip", mysqldb.SelectStrAll)
    if ip == mysqldb.SelectStrAll {
        logs.Error(fmt.Sprintf("input ip error."))
        data := map[string]interface{}{"total": 0, "data": []dbdata.AlertBriefInfo{}}
        m.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input ip error.", data)
        m.ServeJSON()
        return
    }

    name, err := m.parseURIForNameDetail()
    if err != nil {
        logs.Error(fmt.Sprintf("input name error."))
        data := map[string]interface{}{"total": 0, "data": []dbdata.AlertBriefInfo{}}
        m.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input ip error.", data)
        m.ServeJSON()
        return
    }

    w := dbdata.WeaponAlertInfo{}
    alerts, err := w.SelectForAlertBriefInfo(mysqldb.SelectOffsetZero, mysqldb.SelectLimitAll,
        mysqldb.StatusValid, name, ip)
    if err != nil {
        logs.Error(fmt.Sprintf("select error: %s", err.Error()))
        data := map[string]interface{}{"total": 0, "data": []dbdata.AlertBriefInfo{}}
        m.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select error.", data)
        m.ServeJSON()
        return
    }

    data := map[string]interface{}{"total": len(alerts), "data": alerts}
    m.Data["json"] = uhttp.CreateOkResponse(data)
    m.ServeJSON()
    return
}
